package me.zhengjie.modules.finace_taxation.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.finace_taxation.domain.FtOrder;
import me.zhengjie.modules.finace_taxation.domain.FtOrderSchedule;
import me.zhengjie.modules.finace_taxation.service.FtOrderService;
import me.zhengjie.modules.finace_taxation.service.dto.FtOrderDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtOrderQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-30
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "服务订单管理")
@RequestMapping("/api/ft/ftOrder")
public class FtOrderController {

    private final FtOrderService ftOrderService;

    @GetMapping
    @Log("查询服务订单")
    @ApiOperation("查询服务订单")
//    @PreAuthorize("@el.check('ftOrder:list')")
    public ResponseEntity query(@RequestParam Map<String,Object> map, Pageable pageable){
        return new ResponseEntity<>(ftOrderService.queryAll(map,pageable),HttpStatus.OK);
    }

    @PostMapping("/getOrdersByUserId")
    @Log("查询我的服务记录")
    @ApiOperation("查询我的服务记录")
//    @PreAuthorize("@el.check('ftOrder:list')")
    public ResponseEntity getOrdersByUserId(@RequestBody Map<String,Object> map){
        return new ResponseEntity<>(ftOrderService.getOrdersByUserId(map),HttpStatus.OK);
    }

    @GetMapping("/getOrderScheduls/{orderId}")
    @Log("根据订单id查询订单的所有进度")
    @ApiOperation("根据订单id查询订单的所有进度")
//    @PreAuthorize("@el.check('ftOrder:list')")
    public ResponseEntity getOrderScheduls(@PathVariable("orderId") String orderId){
        return new ResponseEntity<>(ftOrderService.getOrderScheduls(orderId),HttpStatus.OK);
    }

    @PostMapping("/updateOrderSchedule")
    @Log("修改订单的进度")
    @ApiOperation("修改订单的进度")
//    @PreAuthorize("@el.check('ftOrder:list')")
    public ResponseEntity updateOrderSchedule(@Validated @RequestBody FtOrderSchedule resources){
        return new ResponseEntity<>(ftOrderService.updateOrderSchedule(resources),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增服务订单")
    @ApiOperation("新增服务订单")
    @PreAuthorize("@el.check('ftOrder:add')")
    public ResponseEntity create(@Validated @RequestBody FtOrderDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftOrderService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("添加订单备注")
    @ApiOperation("添加订单备注")
//    @PreAuthorize("@el.check('ftOrder:edit')")
    public ResponseEntity update(@Validated @RequestBody FtOrderDto resources){
        ftOrderService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除订单")
    @ApiOperation("删除订单")
//    @PreAuthorize("@el.check('ftOrder:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftOrderService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出服务订单")
    @ApiOperation("导出服务订单")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, FtOrderQueryParam query) throws IOException {
        ftOrderService.download(ftOrderService.queryAll(query), response);
    }

}
